﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Utility
{
    public class ChessMessage : BaseMessage
    {
        public string Body;
        public ChessMessageType Type { get; set; }
        public string SenderName { get; set; }

        public ChessMessage()
        {
            Body = "";
            SenderName = "";
        }

        public ChessMessage(byte[] bytes)
        {
            int offset = 0;
            Type = (ChessMessageType) BitConverter.ToInt32(bytes, offset);
            offset += 4;
            int nameSize = BitConverter.ToInt32(bytes, offset);
            offset += 4;
            SenderName = Encoding.UTF8.GetString(bytes, offset, nameSize);
            offset += nameSize;
            int msgSize = BitConverter.ToInt32(bytes, offset);
            offset += 4;
            Body = Encoding.UTF8.GetString(bytes, offset, msgSize);
        }

      
        public override byte[] ToByteArray()
        {
            var result = new List<byte>();
            result.AddRange(BitConverter.GetBytes((int) Type));
            result.AddRange(BitConverter.GetBytes(SenderName.Length));
            result.AddRange(Encoding.UTF8.GetBytes(SenderName));
            result.AddRange(BitConverter.GetBytes(Body.Length));
            result.AddRange(Encoding.UTF8.GetBytes(Body));
            return result.ToArray();
        }
    }
}